<?PHP
defined('_XR_EXEC') or die();
#函数的参数和返回值都是 时间戳
class dates{
	public static function fmtDate($_timestamp){
		return date('Y-m-d H:i:s',$_timestamp);
	}
	/*在_obj基础上再加几天*/
	public static function addDay($_obj,$_num,$_str=false){
		if($_str === true)
		{
			return self::fmtDate(strtotime($_num.' day',$_obj));
		}else{
			return strtotime($_num.' day',$_obj);
		}	
	}
	/*得到这周的第一天*/
	public static function getWeekFirst($_obj,$_str=false){
		$localtime = localtime($_obj,true);
		if($localtime['tm_wday'] == 0){
		  $jianday = 6;
		}else{
			$jianday = $localtime['tm_wday'] - 1;
		}
		return self::addday($_obj,($jianday*-1),$_str);
	}
	/*得到这周的最后一天*/
	public static function getWeekLast($_obj,$_str=false){
		$localtime = localtime($_obj,true);
		if($localtime['tm_wday'] == 0){
		  $jiaday = 0;
		}else{
			$jiaday = 7 - $localtime['tm_wday'];
		}
		return self::addday($_obj,$jiaday,$_str);
	}
	/*在_obj基础上再加几月*/
	public static function addMonth($_obj,$_num,$_str=false){
		$returns = strtotime($_num.' month',$_obj);
		if($_str === true)
		{
			return self::fmtDate($returns);
		}else{
			return $returns;
		}		
	}
	/*得到这个月份的第一天*/
	public static function getMonthFirst($_obj,$_str=false){
		$localtime = localtime ($_obj,true);
		$returns = strtotime(($localtime['tm_year']+1900).'-'.($localtime['tm_mon']+1).'-1');
		if($_str === true)
		{
			return self::fmtDate($returns);
		}else{
			return $returns;
		}		
	}
	/*得到这个月份的最后一天*/
	public static function getMonthLast($_obj,$_str=false){
		$localtime = localtime ( self::addDay(self::getMonthFirst(self::addMonth($_obj,1)) , -1) ,true);
		$returns = strtotime(($localtime['tm_year']+1900).'-'.($localtime['tm_mon']+1).'-'.$localtime['tm_mday'].' 23:59:59');
		if($_str === true)
		{
			return self::fmtDate($returns);
		}else{
			return $returns;
		}		
	}
	
	/*在_obj基础上再加几个季度*/
	public static function addQuarter($_obj,$_num,$_str=false){
		$returns = strtotime(($_num*3).' month',$_obj);
		if($_str === true)
		{
			return self::fmtDate($returns);
		}else{
			return $returns;
		}		
	}
	/*得到这个季度的第一天*/
	public static function getQuarterFirst($_obj,$_str=false){
		$localtime = localtime ($_obj,true);
		switch(($localtime['tm_mon']+1).''){
			case '1':
			case '2':
			case '3':
				$returns =  strtotime(($localtime['tm_year']+1900).'-'.'01-01');
				break;
			case '4':
			case '5':
			case '6':
				$returns =  strtotime(($localtime['tm_year']+1900).'-'.'04-01');
				break;
			case '7':
			case '8':
			case '9':
				$returns =  strtotime(($localtime['tm_year']+1900).'-'.'07-01');
				break;
			case '10':
			case '11':
			case '12':
				$returns = strtotime(($localtime['tm_year']+1900).'-'.'10-01');
				break;
		}
		if($_str === true)
		{
			return self::fmtDate($returns);
		}else{
			return $returns;
		}
	}
	/*得到这个季度的最后一天*/
	public static function getQuarterLast($_obj,$_str=false){
		$localtime = localtime ($_obj,true);
		switch(($localtime['tm_mon']+1).''){
			case '1':
			case '2':
			case '3':
				$returns = self::getMonthLast(strtotime(($localtime['tm_year']+1900).'-'.'03-01'));
				break;
			case '4':
			case '5':
			case '6':
				$returns = self::getMonthLast(strtotime(($localtime['tm_year']+1900).'-'.'06-01'));
				break;
			case '7':
			case '8':
			case '9':
				$returns = self::getMonthLast(strtotime(($localtime['tm_year']+1900).'-'.'09-01'));
				break;
			case '10':
			case '11':
			case '12':
				$returns = self::getMonthLast(strtotime(($localtime['tm_year']+1900).'-'.'12-01'));
				break;
		}
		if($_str === true)
		{
			return self::fmtDate($returns);
		}else{
			return $returns;
		}
	}
	/*在_obj基础上再加几年*/
	public static function addYear($_obj,$_num,$_str=false){
		$returns = self::addMonth($_obj,($_num*12));
		if($_str === true)
		{
			return self::fmtDate($returns);
		}else{
			return $returns;
		}	
	}
	/*得到这年的第一天*/
	public static function getYearFirst($_obj,$_str=false){
		$localtime = localtime ($_obj,true);
		$returns = strtotime(($localtime['tm_year']+1900).'-01-01');
		if($_str === true)
		{
			return self::fmtDate($returns);
		}else{
			return $returns;
		}		
	}
	/*得到这年的最后一天*///
	public static function getYearLast($_obj,$_str=false){
		$localtime = localtime ( self::addDay(self::getYearFirst(self::addYear($_obj,1)) , -1) ,true);
		$returns = strtotime(($localtime['tm_year']+1900).'-12-31 23:59:59');
		if($_str === true)
		{
			return self::fmtDate($returns);
		}else{
			return $returns;
		}		
	}
 

	/* 日期差额 */
	public static function dateDiff($interval, $date1, $date2) {
		 if(!is_string($date1))	$date1 = date('Y-m-d H:i:s',$date1);
		 if(!is_string($date2))	$date2 = date('Y-m-d H:i:s',$date2);
		 $seconds = strtotime($date2) - strtotime($date1);
		 $date1=date("Y-m-d",strtotime($date1));
		 $date2=date("Y-m-d",strtotime($date2));
		 $interval = strtolower(trim($interval)); 
		 switch($interval) {
				 case "y":
						 list($year1, $month1, $day1) = explode('-', $date1);
						 list($year2, $month2, $day2) = explode('-', $date2);
//						 $time1 = (date('H',$date1)*3600) + (date('i',$date1)*60) + (date('s',$date1));
//						 $time2 = (date('H',$date2)*3600) + (date('i',$date2)*60) + (date('s',$date2));
						 $diff = $year2 - $year1; 
//						 if($month1 > $month2) {
//								 $diff -= 1;
//						 } elseif($month1 == $month2) {
//								 if($day1 > $day2) {
//										 $diff -= 1;
//								 } elseif($day1 == $day2) {
//										 if($time1 > $time2) {
//												 $diff -= 1;
//										 }
//								 }
//						 }
						 break;
				 case "m":
						 /* parses the year, month and days. split() was replaced with explode(), PHP Manual says it's faster */
						 list($year1, $month1, $day1) = explode('-', $date1);
						 list($year2, $month2, $day2) = explode('-',$date2); 
						 $time1 = (date('H',$date1)*3600) + (date('i',$date1)*60) + (date('s',$date1));
						 $time2 = (date('H',$date2)*3600) + (date('i',$date2)*60) + (date('s',$date2));
						 $diff = ($year2 * 12 + $month2) - ($year1 * 12 + $month1); 
//						 if($day1 > $day2) {
//								 $diff -= 1;
//						 } elseif($day1 == $day2) {
//								 if($time1 > $time2) {
//										 $diff -= 1;
//								 }
//						 }
						 break;
				 case "w":
						 // Only simple seconds calculation needed from here on
						 $diff = floor($seconds / 604800);
						 break;
				 case "d":
						 $diff = floor($seconds / 86400);
						 break;
				 case "h":
						 $diff = floor($seconds / 3600);
						 break;      
				 case "i":
						 $diff = floor($seconds / 60);
						 break;      
				 case "s":
						 $diff = $seconds;
						 break;      
		 }
		 return $diff;
	}	
	static function dateArea($now,$bdate,$edate){ 
			if(self::dateDiff('s',$bdate,$now) >=0 && self::dateDiff('s',$now,$edate)>=0){
				return true;
			}else{
				return false;
			}
	}
}	
?>